package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;

import ejb.ApplyFacadeEJBRemote;
import ejb.LikeFacadeEJBRemote;
import ejb.TagFacadeEJBRemote;
import ejb.TargetFacadeEJBRemote;
import ejb.UserFacadeEJBRemote;

import model.Myuser;
import model.Tag;
import model.Target;


@WebServlet("/ApplyServlet")
public class ApplyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@PersistenceUnit
	EntityManagerFactory emf;

    @Resource private UserTransaction utx; 

	@PersistenceContext
	EntityManager em;   
	
    @EJB UserFacadeEJBRemote uf;
    @EJB ApplyFacadeEJBRemote af;
    @EJB LikeFacadeEJBRemote lf;
    @EJB TagFacadeEJBRemote tf;
    @EJB TargetFacadeEJBRemote targetf;

	
    public ApplyServlet() {
        super();
        
    }

    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
		out.print("<html><body>");
		HttpSession session=request.getSession();
		String username = (String)session.getAttribute("username");
		String targetid = (String)request.getParameter("targetid");
		if(username==null){
            out.print("Please login first! redirecting to Login...");
            response.setHeader("Refresh","2;URL=login.jsp");
		}
		else if(targetid==null){
			out.print("Please access this page via normal way...");
            response.setHeader("Refresh","2;URL=home.jsp");
		}
		else{
		int tid = Integer.parseInt(targetid.trim());
		
		String reason = (String)request.getParameter("applyform");
		
		out.println(username + " " + targetid + " " + reason);
		
		try{
			utx.begin();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
				
		{
		
		if(targetid!=null && reason!=null){
			
			Myuser u = new Myuser();
			u = uf.findUserByName(username);
			Target t = new Target();
			t = targetf.queryByTargetID(tid);
	
			if(af.ifApplyExist(u, t)==false){
				af.createApply(u, t, reason, "waiting");
			}
			
			out.println("user: " + u.getUsername() + "target: " + t.getTargetname());
			
			boolean l = lf.ifLikeExist(u, t);
			String liked = "";
			if(l==true){
				liked="true";
			}else{
				liked="false";
			}
			
			List<Tag> tag_list = tf.getTagListByTarget(t);
			
			request.setAttribute("taglist", tag_list);
			
			request.setAttribute("liked", liked);
			request.setAttribute("return_t", t);
            request.getRequestDispatcher("Target.jsp").forward(request, response);
			
		}
		
		}
		

		out.print("</html></body>");

		try{
			utx.commit();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		}
		
	
	}

	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
